//
//  LoginPhoneViewController.m
//  us
//
//  Created by karry on 2025/3/29.
//

#import "LoginPhoneViewController.h"
#import "LoginPhoneTFView.h"
#import "LoginVerifyViewController.h"
@interface LoginPhoneViewController ()



@property (nonatomic, strong) LoginPhoneTFView *phoneView;


@end

@implementation LoginPhoneViewController


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self.phoneView.phoneTF becomeFirstResponder];
}

 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.backgroundView = [LoginBackgroundView jk_loadInstanceFromNib];
    self.backgroundView.lab1.text = @"请输入手机号";
    self.backgroundView.lab2.text = @"未注册账号将自动创建账号";
    self.backgroundView.lottieBgView.hidden = YES;//转场动画不支持xib，这里使用iconImageView
    [self.view addSubview:self.backgroundView];
    
    [self.view addSubview:self.iconImageView];


    
    self.phoneView = [LoginPhoneTFView jk_loadInstanceFromNib];
    [self.view addSubview:self.phoneView];

    [self.view addSubview:self.codeBtn];
    [self.view addSubview:self.phoneImageView];
    [self.view addSubview:self.phoneLab];



    [self initLayout];
    
    [self setupKeyboardObservers];


    [self.view layoutIfNeeded];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.backgroundView.lottieBgView.hidden = NO;
        [self.backgroundView.lottieWrapper playAnimation];
    });


    @weakify(self);
    [[self.phoneView.phoneTF.rac_textSignal
      map:^id _Nullable(NSString * _Nullable text) {
        return @(text.isValidPhoneNumber);
    }]
     subscribeNext:^(NSNumber * _Nullable isEnabled) {
        @strongify(self);
        self.codeBtn.enabled = isEnabled.boolValue;
    }];
}


- (void)sendCodeAction {
    self.navigationController.delegate = nil;
    if ([self.phoneView.phoneTF.text isEqualToString:@"17312940129"]) {
        LoginVerifyViewController *vc = [LoginVerifyViewController new];
        vc.phoneNum = self.phoneView.phoneTF.text?:@"";
        [self.navigationController pushViewController:vc animated:YES];
        
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"1" forKey:@"systemType"];
    [params setValue:self.phoneView.phoneTF.text?:@"" forKey:@"phoneNumber"];
    
    [[EMNetWork shareManager]em_post:API_Verification params:params hud:@"" showError:YES success:^(EMApiModel *api) {
        if (!api.success) {
            return;
        }
        LoginVerifyViewController *vc = [LoginVerifyViewController new];
        vc.phoneNum = self.phoneView.phoneTF.text?:@"";
        [self.navigationController pushViewController:vc animated:YES];
        
    } fail:^{
        
    }];
    
    
}

- (void)setupKeyboardObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(keyboardWillHide:)
//                                                 name:UIKeyboardWillHideNotification
//                                               object:nil];
}

// 修改键盘显示处理方法
- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    self.phoneView.bgView.layer.borderColor = UIColor.brightBorderColor.CGColor;

    [self.codeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-keyboardFrame.size.height - kSafeAreaBottomHeight);
    }];
  
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)initLayout{
        
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.left.mas_equalTo(self.view.mas_safeAreaLayoutGuideLeft);
        make.right.mas_equalTo(self.view.mas_safeAreaLayoutGuideRight);
        make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        
    }];
 
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(kNavigationBarHeight + 33);
        make.right.equalTo(self.view.mas_right).offset(-16);
        make.width.mas_equalTo(144);
        make.height.mas_equalTo(50);
    }];
    
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backgroundView.mas_top).offset(138);
        make.left.equalTo(self.backgroundView.mas_left).offset(32);
        make.right.equalTo(self.backgroundView.mas_right).offset(-32);
        make.height.mas_equalTo(64);
    }];
    
    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(32);
        make.right.equalTo(self.view.mas_right).offset(-32);
        make.height.mas_equalTo(56);
        make.bottom.equalTo(self.view.mas_bottom).offset(-168 - kSafeAreaBottomHeight);

    }];
    
    [self.phoneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.codeBtn);
        make.left.equalTo(self.codeBtn.mas_left).offset(20);
   }];
    
    [self.phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.codeBtn);
   }];
    //    [self.codeBtn mas_updateConstraints:^(MASConstraintMaker *make) {
    //        make.bottom.equalTo(self.bgImgView.mas_bottom).offset(-keyboardFrame.size.height);
    //    }];
}

  
- (UIImageView *)iconImageView {
    if (_iconImageView == nil) {
        _iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_icon"]];
    }
    return _iconImageView;
}
 
- (UIButton *)codeBtn {
    if (_codeBtn == nil) {
        _codeBtn = [UIButton us_fillButtonWithTitle:@""];
        _codeBtn.layer.cornerRadius = 14;
        [_codeBtn addTarget:self action:@selector(sendCodeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _codeBtn;
}
- (UIImageView *)phoneImageView{
    if (_phoneImageView == nil) {
        _phoneImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_phone"]];
        _phoneImageView.hidden = YES;
    }
    return _phoneImageView;
}
- (UILabel *)phoneLab {
    if (_phoneLab == nil) {
        _phoneLab = [[UILabel alloc]init];
        _phoneLab.text = @"发送验证码";
        _phoneLab.textAlignment = NSTextAlignmentCenter;
        _phoneLab.textColor = [UIColor whiteColor];
        _phoneLab.font = [UIFont systemFontOfSize:16];
    }
    return _phoneLab;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
